<%@ page language="java" pageEncoding="utf-8"%>
<%@ include file="/commons/taglibs.jsp"%>
<html>
<head>
<title>角色管理-授权用户</title>
<%@ include file="/commons/meta.jsp" %>
<%@ include file="/commons/header.jsp" %>
<%@ include file="/extjs/jsps/importExt.jsp" %>
<%@ include file="/extjs/jsps/importEasyExt.jsp" %>
<script language="javascript" type="text/javascript">
function closeCurrentWindow() {
	var winId='${winId}';
	var closeWin=Ext.WindowMgr.get(winId);
	if(closeWin==null) {
		closeWin=parent.Ext.WindowMgr.get(winId);
	}
	if(closeWin==null) {
		window.returnValue=false;
		window.close();
	}else {
		closeWin.returnValue=false;
		closeWin.close();
	}
}
var store = new Ext.data.JsonStore({
	autoDestroy: true,
	autoLoad: true,
	remoteSort: true,
	url: '${ctx}/role!listUserJSON.action',
   	root: 'records',
   	baseParams: {
   		id: '${id}'
   	},
	fields: [
        {name: 'id',mapping: 'id',type: 'string'},
        {name: 'loginName',mapping: 'loginName',type: 'string'},
        {name: 'userName',mapping: 'userName',type: 'string'},
        {name: 'hasRole',mapping: 'hasRole',type: 'boolean'}
	]
});

var sm = new Ext.grid.CheckboxSelectionModel({
	singleSelect: false
});

var cm = new Ext.grid.ColumnModel({
	defaults: {
		align: 'center',
		sortable: true
	},
	columns: [
		new Ext.grid.RowNumberer(),
		sm,
		{header: '登录名',dataIndex: 'loginName',width: 40},
		{header: '姓名',dataIndex: 'userName',width: 40},
		{header: '是否授权',dataIndex: 'hasRole',width: 30,
			renderer: function(value,metaData,record,rowIndex,colIndex,store) {
				return value==true?'<span style="color:red;">已授权</span>':'未授权';
			}
		}
	]
});

var toolbarItem = [
	'-',{
		width: 65,
		xtype: 'combo',
		mode: 'local',
		triggerAction: 'all',
		valueField: 'value',
		displayField: 'text',
		editable: false,
	    store: new Ext.data.ArrayStore({
	        fields: ['value','text'],
	        data: [['','全部'],[true,'已授权'],[false,'未授权']]
	    }),
	    value: '',
		listeners: {
			select: function(thiz,record,index) {
				store.setBaseParam("hasRole", thiz.getValue());
				store.load();
			}
		}
	},'-',{
		xtype: 'textfield', 
		name: 'userName',
		emptyText: '输入关键字按回车',
		cls: 'SearchFieldCls',
		enableKeyEvents: true,
		listeners: {
			keyup: function(thiz, eventObj) {
				store.setBaseParam("userName",thiz.getValue());
				if (eventObj.getKey()==eventObj.ENTER) {
					store.load();
				}
			}
		}
	}
];

function authRoleUser(action) {
	if (sm.getCount()==0) {
		Common.showErrorMsg('请至少选择一条记录！');
		return;
	}
	Common.ajaxRequest('${ctx}/role!'+action+'.action',{
		id: '${id}',
		userIds: Common.getSelectedProperties(sm,'id')
	},function(success) {
		if(success) {
			store.load();
		}
	});
}

var gridPanel = new Ext.grid.EditorGridPanel({
	style: 'text-align:left;',
	border: false,
	stripeRows: true,// 斑马线效果
	loadMask: {
		msg: '加载数据中, 请稍侯... ...'
	},
	trackMouseOver: true,
	store: store,
	cm: cm,
	sm: sm,
	autoExpandColumn: 'userName',
	clicksToEdit: 1,// 单击编辑
	viewConfig: {
		scrollOffset: 20,// 滑动条宽度，默认是20
		forceFit: true
	},
	tbar: Common.pageingToolBar(new EasyExt.widgets.PageSizePlugin(),store,toolbarItem),
	bbar: [{
		text: '授权',
		iconCls: 'acceptBtn',
		handler: function() {
			authRoleUser('addUserJSON');
		}
	},'-',{
		text: '取消授权',
		iconCls: 'cancelBtn',
		handler: function() {
			authRoleUser('removeUserJSON');
		}
	},'-',{
		text: '取消',
		iconCls: 'delete2Btn',
		handler: function() {
			closeCurrentWindow();
		}
	}]
});
Ext.onReady(function() {
	var viewport = new Ext.Viewport({
		layout: 'fit',
		items: [gridPanel]
	});
});
</script>
</head>

<body>
</body>
</html>